package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Cart;
import com.example.domain.CartVO;
import com.example.domain.User;
import com.example.result.JsonResult;
import com.example.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;
    //添加商品到购物车
    @GetMapping("/insert")
    public JsonResult insert(Cart cart, HttpSession session){
        User user = getSessionUser(session);
        if(user==null){
            throw new RuntimeException("请先登录");
        }
        QueryWrapper<Cart> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",user.getUid());
        wrapper.eq("pid",cart.getPid());
        Cart one = cartService.getOne(wrapper);
        if(one!=null){
            throw new RuntimeException("已添加到购物车,请在购物车页面修改或者结算");
        }

        cart.setUid(user.getUid());

        cart.setCreated_user(user.getUsername());
        cart.setCreated_time(new Date());
        cart.setModified_time(new Date());
        cart.setModified_user(user.getUsername());
        boolean flag = cartService.save(cart);
        if(!flag){
            throw new RuntimeException("添加失败");
        }
        return new JsonResult(true,"添加成功");
    }
    //根据商品id（pid）获取购物车信息
    @GetMapping("/getCart")
    public JsonResult getCart(Long pid){
        QueryWrapper<Cart> wrapper=new QueryWrapper<>();
        wrapper.eq("pid",pid);
        Cart cart = cartService.getOne(wrapper);
        if(cart==null){
            throw new RuntimeException("未加入购物车");
        }
        return new JsonResult(true,"");
    }
    //根据商品id（pid）删除信息
    @GetMapping("/delete/{pid}")
    public JsonResult delete(@PathVariable Long pid){
        QueryWrapper<Cart> wrapper=new QueryWrapper<>();
        wrapper.eq("pid",pid);
        boolean flag = cartService.remove(wrapper);
        if(!flag){
            throw new RuntimeException("操作失败，请重试");
        }
        return new JsonResult(true,"购物车已取消");
    }
    //购物车页面获取所有的购物车信息
    @PostMapping("/getPageList")
    public JsonResult getPageList(HttpSession session){
        List<CartVO> list = cartService.PageCartVO(getSessionUId(session));

        return new JsonResult(true,"",list);
    }
    //行删除
    @GetMapping("/delCartItem/{cid}")
    public JsonResult delCartItem(@PathVariable Long cid){
        QueryWrapper<Cart> wrapper=new QueryWrapper<>();
        wrapper.eq("cid",cid);
        Cart one = cartService.getOne(wrapper);
        if(one==null){
            throw new RuntimeException("信息有误，请刷新");
        }
        boolean flag = cartService.remove(wrapper);
        if(!flag){
            throw new RuntimeException("删除失败");
        }
        return new JsonResult(true,"删除成功");
    }
    //集合删除
    @PostMapping("/deleteList")
    public JsonResult deleteList(@RequestBody List<Long> list){
        for (Long cid : list) {
            QueryWrapper<Cart> wrapper=new QueryWrapper<>();
            wrapper.eq("cid",cid);
            boolean flag = cartService.remove(wrapper);
            if(!flag){
                throw new RuntimeException("删除失败，请重试");
            }
        }

        return new JsonResult(true,"删除成功");
    }
    //增加或减少购物车中商品的num
    @PutMapping("/handleChange/{cid}/{num}")
    public JsonResult handleChange(@PathVariable Long cid,@PathVariable Long num){
        QueryWrapper<Cart> wrapper=new QueryWrapper<>();
        wrapper.eq("cid",cid);
        Cart one = cartService.getOne(wrapper);
        if(one==null){
            throw new RuntimeException("信息有误，请刷新");
        }
        UpdateWrapper<Cart> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("cid",cid);  //条件
        updateWrapper.set("num",num);   //修改后的值
        boolean flag = cartService.update(updateWrapper);
        if(!flag){
            throw new RuntimeException("添加失败,请重试");
        }
        return new JsonResult(true,"");
    }

    //根据cids数组获取购物车信息
    @PostMapping("/getListCids")
    public JsonResult getListCids(@RequestBody List<Long> cids){
        List<CartVO> list = cartService.getListCids(cids);
        if(list==null){
            throw new RuntimeException("获取失败，请重试");
        }

        return new JsonResult(true,null,list);
    }
}
